layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery;
    form.on("submit(login)", function (data) {
        $.ajax({
            "url": getRootPath_web() + "/admin/sys/login.do",
            "type": "POST",
            "dataType": "json",
            "data": {
                username:$("#username").val(),
                password:encryptPassWord($("#password").val()),
                captcha:$("#captcha").val()
            },
            "success": function (ret) {
                if (ret.code == 0) {
                    window.location.href = getRootPath_web() + "/html/index.html";
                } else {
                    layer.alert(ret.msg);
                    refreshCode();
                }
            }
        });
        return false;
    });

    function encryptPassWord(password){
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdTQP4U8zoVraB5nh87eEzgzUTzC0br4kASf0wKoO+xJIZLN9sEFNGbx4ItkhxgQTpej1F86kU5FH7p/JQqrvuh8RvYV9kUSVk3N191kkcODR2YIroQXqJrt0NZWy5e8xkv5Hy4av67f4FhPH44EWv4hUF0XTZ0bQtug9jnhyjaQIDAQAB");
      var passWord = encodeURI(encrypt.encrypt(password)).replace(/\+/g, '%2B');
        return passWord;
    }

    $(function () {
        var timestamp = new Date().getTime();
        $('#captchaImg').attr("src", getRootPath_web() + "/admin/sys/captcha.jpg?t=" + timestamp);
    });

//表单输入效果
    $(".loginBody .input-item").click(function (e) {
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function () {
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function () {
        $(this).parent().removeClass("layui-input-focus");
        if ($(this).val() != '') {
            $(this).parent().addClass("layui-input-active");
        } else {
            $(this).parent().removeClass("layui-input-active");
        }
    })
});

function refreshCode() {
    var timestamp = new Date().getTime();
    $('#captchaImg').attr("src", getRootPath_web() + "/admin/sys/captcha.jpg?t=" + timestamp);
}